﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using System.Drawing;

namespace mveEngine
{
	public class ImageUtil
	{
		
		public static string GetLocalImagePath(string ImgPath)
		{
			if(!string.IsNullOrEmpty(ImgPath))
			{
				var localPath = GetLocalImgPath(ImgPath);

				if (File.Exists(ImgPath) && ImgPath.IndexOf(ApplicationPaths.AppImagePath) == 0)
					return ImgPath;
				if (File.Exists(localPath))
					return localPath;
				
				if  (!ImgPath.StartsWith("http://") &&
					 File.Exists(ImgPath))
				{
					File.Copy(ImgPath, localPath);
					BitmapMetadataHelper.WriteMetadata(localPath, TypeBitmapMetadata.UniqueId, Helper.GetMD5(ImgPath));
					return localPath;
				}
				if (ImgPath.StartsWith("http://") && DownloadUsingRetry(ImgPath))
				{
					BitmapMetadataHelper.WriteMetadata(localPath, TypeBitmapMetadata.UniqueId, Helper.GetMD5(ImgPath));
					return localPath;
				}
			}
			
			return string.Empty;
		}

		public static bool LocalFileExists(string ImgPath, out string localPath)
		{
			localPath = "";
			if (string.IsNullOrEmpty(ImgPath))
			{
				return false;
			}

			if (File.Exists(ImgPath))
			{
				localPath = ImgPath;
			}
			else
			{
				// Coolweb : improve performance, call only one time GetLocalImgPath, stock result in temp variable
				string tempPath = GetLocalImgPath(ImgPath);
				if (File.Exists(tempPath))
				{
					localPath = tempPath;
				}
			}
			return !string.IsNullOrEmpty(localPath);
		}

		static string GetLocalImgPath(string ImgPath)
		{
			return Path.Combine(ApplicationPaths.AppImagePath, Helper.GetMD5(ImgPath) + Path.GetExtension(ImgPath));
		}

		static bool DownloadUsingRetry(string ImgPath)
		{
			int attempt = 0;
			bool success = false;
			while (attempt < 2)
			{
				try
				{
					attempt++;
					DownloadImage(ImgPath);
					success = true;
					break;
				}
				catch
				{
				}
			}
			return success;
		}

		static void DownloadImage(string ImgPath)
		{
			HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(ImgPath);
			req.Timeout = 30000;
			
			var wp = WebRequest.DefaultWebProxy;
			wp.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
			req.Proxy = wp;
			
			using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
				using (MemoryStream ms = new MemoryStream())
			{
				Stream r = resp.GetResponseStream();
				int read = 1;
				byte[] buffer = new byte[10000];
				while (read > 0)
				{
					read = r.Read(buffer, 0, buffer.Length);
					ms.Write(buffer, 0, read);
				}
				ms.Flush();
				ms.Seek(0, SeekOrigin.Begin);


				using (var stream = ProtectedFileStream.OpenExclusiveWriter(GetLocalImgPath(ImgPath)))
				{
					stream.Write(ms.ToArray(), 0, (int)ms.Length);
				}
				ms.Close();
			}
		}

		public static Image SetImgOpacity(Image imgPic, float imgOpac)
		{

			Bitmap bmpPic = new Bitmap(imgPic.Width, imgPic.Height);
			Graphics gfxPic = Graphics.FromImage(bmpPic);
			System.Drawing.Imaging.ColorMatrix cmxPic = new System.Drawing.Imaging.ColorMatrix();
			cmxPic.Matrix33 = imgOpac;
			System.Drawing.Imaging.ImageAttributes iaPic = new System.Drawing.Imaging.ImageAttributes();
			iaPic.SetColorMatrix(cmxPic, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
			gfxPic.DrawImage(imgPic, new Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, iaPic);
			gfxPic.Dispose();
			return bmpPic;
		}
		
		public static void DeleteLocalCacheImage(string imagePath)
		{
			var localPath = GetLocalImgPath(imagePath);

			if (File.Exists(localPath))
			{
				File.Delete(localPath);
			}
		}
	}
}